﻿#include "user_page_config.h"

//lv_obj_t* btn;

static lv_obj_t* m_btn[2];  //左右按键
static lv_obj_t* m_lab[2];  //左右按键显示字符




 //lv_obj_t* page_config.window_obj;
 static lv_style_t msgbox_style;         //外部样式


static const char  chinese_dev_low_char[15] =
{
    "设备电量低",
};


static const char  english_dev_low_char[15] =
{
    "Low Battery",
};

static const char  chinese_paired_fail_char[15] =
{
    "配对失败",
};
static const char  english_paired_fail_char[15] =
{
    "Pairing Failed"
};


static const char  chinese_paired_success_char[15] =
{
    "配对完成",
};
static const char  english_paired_success_char[20] =
{
    "Pairing Completed"
};


static const char  chinese_add_dev_char[4][20] =
{
    "添加设备 01",
    "添加设备 02",
    "添加设备 03",
    "添加设备 04",
};
static const char  english_add_dev_char[4][20] =
{
    "ADD Device 01",
    "ADD Device 02",
    "ADD Device 03",
    "ADD Device 04",
};




static const char  chinese_reset_factory_char[20] =
{
    "重置为出厂设置?",
};
static const char  english_reset_factory_char[30] =
{
    "Restore factory settings?"
};



static const char  chinese_enter_char[20] =
{
    "确认",
};
static const char  english_enter_char[20] =
{
    "YES"
};
static const char  chinese_canel_char[20] =
{
    "取消",
};
static const char  english_canel_char[20] =
{
    "NO"
};



static const char  chinese_choice_char[2][20] =
{
    "确认",
    "取消",
};
static const char  english_choice_char[2][20] =
{
    "YES",
    "NO",
};





static void window_event_handler(lv_event_t* e)
{
	
}

//显示弹窗函数

static void msgbox_event_callback(lv_event_t * e)
{
#if 0
   
#else

  

    lv_event_code_t event = lv_event_get_code(e);
    //lv_obj_t *obj = lv_event_get_target(e);
    lv_obj_t* obj = e->user_data;
    if (!lv_obj_is_valid(obj))
        return;
    if (event == LV_EVENT_DELETE)
    {
        printf("LV_EVENT_DELETE\r\n");

    }
    else if (event == LV_EVENT_CLICKED)
    {

        lv_obj_t* obj = lv_event_get_current_target(e);
        uint32_t id = lv_obj_get_index(obj); //获取界面索引选中跳入界面
        printf("LV_EVENT_CLICKED event=%d,child %d\r\n", event, id);
        extern lv_obj_t* prj_prev_cont;
        extern lv_obj_t* prj_parent_cont;
        if ((page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_YES)||
            (page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_NO))
            user_window_page_exit(lv_obj_get_parent(obj));

      else
            user_window_page_exit((obj));

        extern lv_indev_t* g_indev_keypad;
        //g_group = lv_group_create();
        lv_indev_set_group(g_indev_keypad, g_group);
        lv_group_set_default(g_group);         //设置组(group)为默认模式,后面的部件创建时会自动加入组(group)

        //user_setting_page_exit(prj_prev_cont);
        //prj_prev_cont = lv_obj_create(prj_parent_cont);

        //printf("lv_obj_get_state = %d\r\n", lv_obj_get_state(prj_prev_cont));

        //user_setting_page_entry(prj_prev_cont);

        //lv_group_add_obj(g_group, prj_prev_cont);

        //prj_prev_cont = lv_obj_create(prj_parent_cont);

        //确认/取消按键事件处理
        switch (id)
        {

            case DEFAULT_CHOICE_BTN_LEFT:
                printf("---------------------DEFAULT_CHOICE_BTN_LEFT-------------------1\r\n");
                break;
            case DEFAULT_CHOICE_BTN_RIGHT:
                printf("---------------------DEFAULT_CHOICE_BTN_RIGHT-------------------\r\n");
                break;
        default:break;
        }
    }

    else if (event == LV_EVENT_FOCUSED)
    {
        printf("%s LV_EVENT_FOCUSED\r\n",__FUNCTION__);

            if (
                (page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_NO) ||
                (page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_YES)
               )   
       {
                lv_obj_t* obj = lv_event_get_current_target(e);
                printf("lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(obj));
                if (obj != NULL)
                {
                    uint32_t id = lv_obj_get_index((obj)); //获取界面索引选中跳入界面
                    if (id > 2)
                    {
                        id = 0;
                    }
                    lv_obj_set_style_border_color(m_btn[id], lv_color_make(0xff, 0xff, 0xff), 0);
                    //lv_obj_add_state(m_btn[id], LV_STATE_FOCUS_KEY|LV_STATE_FOCUSED|LV_STATE_PRESSED);

                    printf("LV_EVENT_FOCUSED event=%d,id %d\r\n", event, id);

                    lv_group_focus_obj(m_btn[id]);

                }
        }
       
    }
    else if (event == LV_EVENT_DEFOCUSED)
    {
        printf("%s LV_EVENT_DEFOCUSED\r\n", __FUNCTION__);
        if (
            (page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_NO) ||
            (page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_YES)
            )
        {
            lv_obj_t* obj = lv_event_get_current_target(e);
            //printf("lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(obj));
            if (obj != NULL)
            {
                uint32_t id = lv_obj_get_index((obj)); //获取界面索引选中跳入界面
                if (id > 2)
                {
                    id = 0;
                }
                lv_obj_set_style_border_color(m_btn[id], lv_color_make(0x00, 0x00, 0x00), 0);
                //printf("LV_EVENT_DEFOCUSED event=%d,id %d\r\n", event, id);
                //lv_obj_clear_state(obj, LV_STATE_FOCUS_KEY|LV_STATE_FOCUSED|LV_STATE_PRESSED);    /*Remove the pressed state manually*/

            }
        }
        

    }
    else if (event == LV_EVENT_KEY)
    {
        printf("LV_EVENT_KEY event=%d\r\n", event);
    }
    else if (event == LV_EVENT_WINDOW)
    {
        //printf("LV_EVENT_MENU_CHANGE event=%d\r\n", event);


        lv_obj_t* obj = lv_event_get_current_target(e);
        uint32_t id = lv_obj_get_index(obj); //获取界面索引选中跳入界面
        printf("LV_EVENT_WINDOW event=%d,child %d\r\n", event, id);


        uint32_t* window_type = lv_event_get_param(e);
        printf("LV_EVENT_WINDOW window_type code %d\r\n", *window_type);
        page_config.window_type = *window_type;


            lv_obj_t* parent_obj = NULL; 
            //if ((page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_YES) ||
            //    (page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_NO))
            //    parent_obj = lv_obj_get_parent(obj);
           // else {
                parent_obj = obj;
           // }

        Window_Display_Callback(page_config.window_type, parent_obj);
    }



#endif
}



static void event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_current_target(e);
    LV_LOG_USER("Button %s clicked", lv_msgbox_get_active_btn_text(obj));
}

//显示是否关机提示窗

/*
    进入弹窗界面 释放内存
*/
void user_window_page_entry(lv_obj_t* obj, WINDOW_Type_t type)
{
    //if (page_config.window_type == DISPLAY_WINDOW_OFF)
    //{
    //    return;
    //}
    printf("%s\r\n", __FUNCTION__);
    page_config.window_is_runing = true;

    //page_config.current_page = WINDOW_MENU;
    extern lv_indev_t* g_indev_keypad;
    page_config.window_group = lv_group_create();
    lv_group_set_default(page_config.window_group);
    lv_indev_set_group(g_indev_keypad, page_config.window_group);

    lv_obj_set_style_pad_all(obj, 0, 0);
    lv_obj_set_style_border_width(obj, 0, 0);
    lv_obj_set_style_bg_color(obj, lv_color_black(), 0);
    lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100));
    lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF);


    page_config.window_obj = lv_obj_create(obj/*lv_scr_act()*/);



    lv_style_init(&msgbox_style);           // 初始化按钮样式
    lv_style_set_radius(&msgbox_style, 20); // 设置样式圆角弧度


    lv_obj_align(page_config.window_obj, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(page_config.window_obj, 200, 120);
    lv_obj_align(page_config.window_obj, LV_ALIGN_CENTER, 0, 0);

    lv_obj_add_style(page_config.window_obj, &msgbox_style, 0);
    lv_obj_set_style_bg_color(page_config.window_obj, lv_color_make(0, 0, 0), 0);//设置背景颜色为黑色
    lv_obj_set_scrollbar_mode(page_config.window_obj, LV_SCROLLBAR_MODE_OFF);  //取消滚动条显示
    lv_obj_set_style_bg_opa(page_config.window_obj, LV_OPA_60, 0);               //设置透明度



        if ((type != DISPLAY_WINDOW_RESET_DEFAULT_NO) && (type != DISPLAY_WINDOW_RESET_DEFAULT_YES))
        {
            if (type == DISPLAY_WINDOW_LOW_BATTERY)
            {


                m_lab[0] = lv_label_create(page_config.window_obj);

                lv_label_set_recolor(m_lab[0], true);                        /*Enable re-coloring by commands in the text*/
                lv_obj_set_style_text_font(m_lab[0], &font18, 0);
                lv_obj_set_style_text_color(m_lab[0], lv_color_make(0xff, 0xff, 0xff), 0);


                if (page_config.language_type == CHINESE)
                {
                    lv_label_set_text(m_lab[0], &chinese_dev_low_char[0]);
                    lv_obj_center(m_lab[0]);
                }
                else if (page_config.language_type == ENGLISH)
                {
                    lv_label_set_text(m_lab[0], &english_dev_low_char[0]);
                    lv_obj_center(m_lab[0]);
                }
            }
            else if (type == DISPLAY_WINDOW_PAIRED_FAIL)
            {


                m_lab[0] = lv_label_create(page_config.window_obj);

                lv_label_set_recolor(m_lab[0], true);                        /*Enable re-coloring by commands in the text*/
                lv_obj_set_style_text_font(m_lab[0], &font18, 0);
                lv_obj_set_style_text_color(m_lab[0], lv_color_make(0xff, 0xff, 0xff), 0);


                if (page_config.language_type == CHINESE)
                {
                    lv_label_set_text(m_lab[0], &chinese_paired_fail_char[0]);
                    lv_obj_center(m_lab[0]);
                }
                else if (page_config.language_type == ENGLISH)
                {
                    lv_label_set_text(m_lab[0], &english_paired_fail_char[0]);
                    lv_obj_center(m_lab[0]);
                }
            }
            else if (type == DISPLAY_WINDOW_ADD_DEV1)
            {


                m_lab[0] = lv_label_create(page_config.window_obj);

                lv_label_set_recolor(m_lab[0], true);                        /*Enable re-coloring by commands in the text*/
                lv_obj_set_style_text_font(m_lab[0], &font18, 0);
                lv_obj_set_style_text_color(m_lab[0], lv_color_make(0xff, 0xff, 0xff), 0);


                if (page_config.language_type == CHINESE)
                {
                    lv_label_set_text(m_lab[0], chinese_add_dev_char[0]);
                    //printf
                    lv_obj_center(m_lab[0]);
                }
                else if (page_config.language_type == ENGLISH)
                {
                    lv_label_set_text(m_lab[0], english_add_dev_char[0]);
                    lv_obj_center(m_lab[0]);
                }
            }
            else if (type == DISPLAY_WINDOW_ADD_DEV2)
            {


                m_lab[0] = lv_label_create(page_config.window_obj);

                lv_label_set_recolor(m_lab[0], true);                        /*Enable re-coloring by commands in the text*/
                lv_obj_set_style_text_font(m_lab[0], &font18, 0);
                lv_obj_set_style_text_color(m_lab[0], lv_color_make(0xff, 0xff, 0xff), 0);


                if (page_config.language_type == CHINESE)
                {
                    lv_label_set_text(m_lab[0], chinese_add_dev_char[1]);
                    //printf
                    lv_obj_center(m_lab[0]);
                }
                else if (page_config.language_type == ENGLISH)
                {
                    lv_label_set_text(m_lab[0], english_add_dev_char[1]);
                    lv_obj_center(m_lab[0]);
                }
            }
            else if (type == DISPLAY_WINDOW_ADD_DEV3)
            {


                m_lab[0] = lv_label_create(page_config.window_obj);

                lv_label_set_recolor(m_lab[0], true);                        /*Enable re-coloring by commands in the text*/
                lv_obj_set_style_text_font(m_lab[0], &font18, 0);
                lv_obj_set_style_text_color(m_lab[0], lv_color_make(0xff, 0xff, 0xff), 0);


                if (page_config.language_type == CHINESE)
                {
                    lv_label_set_text(m_lab[0], chinese_add_dev_char[2]);
                    //printf
                    lv_obj_center(m_lab[0]);
                }
                else if (page_config.language_type == ENGLISH)
                {
                    lv_label_set_text(m_lab[0], english_add_dev_char[2]);
                    lv_obj_center(m_lab[0]);
                }
            }
            else if (type == DISPLAY_WINDOW_ADD_DEV4)
            {
                m_lab[0] = lv_label_create(page_config.window_obj);

                lv_label_set_recolor(m_lab[0], true);                        /*Enable re-coloring by commands in the text*/
                lv_obj_set_style_text_font(m_lab[0], &font18, 0);
                lv_obj_set_style_text_color(m_lab[0], lv_color_make(0xff, 0xff, 0xff), 0);


                if (page_config.language_type == CHINESE)
                {
                    lv_label_set_text(m_lab[0], chinese_add_dev_char[3]);
                    //printf
                    lv_obj_center(m_lab[0]);
                }
                else if (page_config.language_type == ENGLISH)
                {
                    lv_label_set_text(m_lab[0], english_add_dev_char[3]);
                    lv_obj_center(m_lab[0]);
                }
            }
            else if (type == DISPLAY_WINDOW_PAIRED_SUCCESS)
            {
                m_lab[0] = lv_label_create(page_config.window_obj);

                lv_label_set_recolor(m_lab[0], true);                        /*Enable re-coloring by commands in the text*/
                lv_obj_set_style_text_font(m_lab[0], &font18, 0);
                lv_obj_set_style_text_color(m_lab[0], lv_color_make(0xff, 0xff, 0xff), 0);


                if (page_config.language_type == CHINESE)
                {
                    lv_label_set_text(m_lab[0], &chinese_paired_success_char[0]);
                    lv_obj_center(m_lab[0]);
                }
                else if (page_config.language_type == ENGLISH)
                {
                    lv_label_set_text(m_lab[0], &english_paired_success_char[0]);
                    lv_obj_center(m_lab[0]);
                }
            }


            lv_obj_add_event_cb(page_config.window_obj, msgbox_event_callback, LV_EVENT_ALL, page_config.window_obj);
            lv_obj_add_event_cb(page_config.window_obj, msgbox_event_callback, LV_EVENT_WINDOW, page_config.window_obj);

            //lv_group_add_obj(page_config.window_group, page_config.window_obj);


            return;
      


        }
        else  //带按键的选择框
        {







        for (int i = 0; i < 2; i++)
        {
            m_btn[i] = lv_btn_create(page_config.window_obj); // 创建一个按钮
            lv_obj_remove_style_all(m_btn[i]);
            lv_obj_set_style_pad_all(m_btn[i], 0, 0);
            lv_obj_set_style_border_width(m_btn[i], 1, 0);
            lv_obj_set_style_border_color(m_btn[i], lv_color_make(0x0, 0x00, 0x00), 0);
            //lv_obj_set_style_pad_all(m_btn[i], 1, 0);
            //lv_obj_set_style_border_color(m_btn[i], lv_color_make(0xff, 0xff, 0xff), 0);
            lv_obj_set_style_bg_color(m_btn[i], lv_color_black(), 0);


       

            if (m_btn[i] != NULL)
            {
                m_lab[i] = lv_label_create(m_btn[i]); // 创建一个标签
                lv_label_set_recolor(m_lab[i], true);                        /*Enable re-coloring by commands in the text*/
                lv_obj_set_style_text_font(m_lab[i], &font18, 0);
                lv_obj_set_style_text_color(m_lab[i], lv_color_make(0xff, 0xff, 0xff), 0);

                if (i == 0)
                {
                    //lv_obj_align(m_lab[i], LV_ALIGN_CENTER, 0,0);

                   //lv_obj_align(m_btn[i], LV_ALIGN_BOTTOM_LEFT,30 , -10);
                    lv_obj_align_to(m_btn[i], page_config.window_obj, LV_ALIGN_BOTTOM_LEFT, 30, -10);
                }

                if (i == 1)
                {
                    //lv_obj_align(m_lab[i], LV_ALIGN_CENTER, 0, 0);
                   //lv_obj_align(m_btn[i], LV_ALIGN_BOTTOM_RIGHT, -30, -10);
                    lv_obj_align_to(m_btn[i], page_config.window_obj, LV_ALIGN_BOTTOM_RIGHT, -30, -10);

                }





                if (page_config.language_type == CHINESE)
                {
                    lv_label_set_text(m_lab[i], chinese_choice_char[i]);
                }
                else if (page_config.language_type == ENGLISH)
                {
                    lv_label_set_text(m_lab[i], english_choice_char[i]);
                    lv_obj_center(m_lab[i]);
                }

                lv_obj_add_flag(m_btn[i], LV_OBJ_FLAG_CHECKABLE | LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_CLICK_FOCUSABLE);
                //lv_obj_add_flag(m_btn[i], LV_OBJ_FLAG_SCROLL_ON_FOCUS);

                lv_obj_add_state(m_btn[i], LV_STATE_FOCUSED); /*To be sure the cursor is visible*/
                lv_obj_add_event_cb(m_btn[i], msgbox_event_callback, LV_EVENT_ALL, m_btn[i]);
                //lv_obj_add_event_cb(m_btn[i], msgbox_event_callback, LV_EVENT_ALL, m_btn[i]);

                lv_obj_clear_flag(m_btn[i], LV_OBJ_FLAG_EVENT_BUBBLE);
                //lv_obj_set_pos(m_lab, 102, 30 + i * 48);
                lv_group_add_obj(page_config.window_group, m_btn[i]);

            }
        }

        //默认选中的选项

            if (type == DISPLAY_WINDOW_RESET_DEFAULT_NO)
            {
                lv_group_focus_obj(m_btn[DEFAULT_CHOICE_BTN_RIGHT]);
                lv_obj_set_style_border_color(m_btn[DEFAULT_CHOICE_BTN_RIGHT], lv_color_make(0xff, 0xff, 0xff), 0);
            }
            else if (type == DISPLAY_WINDOW_RESET_DEFAULT_YES)
            {
                lv_group_focus_obj(m_btn[DEFAULT_CHOICE_BTN_LEFT]);
                lv_obj_set_style_border_color(m_btn[DEFAULT_CHOICE_BTN_LEFT], lv_color_make(0xff, 0xff, 0xff), 0);
            }





            lv_obj_t* title = lv_label_create(page_config.window_obj);

            lv_label_set_recolor(title, true);                        /*Enable re-coloring by commands in the text*/
            lv_obj_set_style_text_font(title, &font18, 0);
            lv_obj_set_style_text_color(title, lv_color_make(0xff, 0xff, 0xff), 0);

            //page_config.language_type = ENGLISH;
            if (page_config.language_type == CHINESE)
            {
                lv_label_set_text(title, &chinese_reset_factory_char[0]);
                lv_obj_center(title);
            }
            else if (page_config.language_type == ENGLISH)
            {
                lv_label_set_text(title, &english_reset_factory_char[0]);
                lv_obj_center(title);
            }


            //lv_group_add_obj(page_config.window_group, page_config.window_obj);
            lv_obj_add_event_cb(page_config.window_obj, msgbox_event_callback, LV_EVENT_WINDOW, page_config.window_obj);

            //lv_obj_add_event_cb(page_config.window_obj, msgbox_event_callback, LV_EVENT_CLICKED, page_config.window_obj);
            lv_obj_add_event_cb(m_btn[0], msgbox_event_callback, LV_EVENT_CLICKED, m_btn[0]);
            lv_obj_add_event_cb(m_btn[1], msgbox_event_callback, LV_EVENT_CLICKED, m_btn[1]);


            //lv_obj_add_event_cb(page_config.window_obj, msgbox_event_callback, LV_EVENT_ALL, page_config.window_obj);
            //lv_group_add_obj(page_config.window_group, page_config.window_obj);
            //lv_obj_add_event_cb(page_config.window_obj, msgbox_event_callback, LV_EVENT_ALL, page_config.window_obj);
            //lv_group_add_obj(page_config.window_group, page_config.window_obj);
            printf("grpup num %d\r\n", lv_group_get_obj_count(page_config.window_group));
       
        }
  
}




/*
    退出主页界面 释放内存
*/
void user_window_page_exit(lv_obj_t* obj)
{
    if (lv_group_get_obj_count(page_config.window_group) > 0)
    {
        printf("grpup num %d\r\n", lv_group_get_obj_count(page_config.window_group));
        lv_group_remove_all_objs(page_config.window_group);
    }
    //lv_obj_del(obj);
    lv_obj_del((obj));
    printf(" page_config.current_page = %d\r\n", page_config.current_page);
    //page_config.current_page = page_config.last_page ;

    //page_config.last_page = WINDOW_MENU;
    printf(" page_config.current_page = %d,last_page =%d\r\n", page_config.current_page, page_config.last_page);

    page_config.window_is_runing = false;

    lv_group_del(page_config.window_group);
}
